//
//  XSJComposeTextView.swift
//  weiBo项目准备
//
//  Created by 鲜世杰 on 16/5/19.
//  Copyright © 2016年 鲜世杰. All rights reserved.
//

import UIKit

class XSJComposeTextView: UITextView {
    private lazy var placeHolderLabel : UILabel = {
    
    let label = UILabel()
        label.font = UIFont.systemFontOfSize(12)
        label.text = "我静静的尝一口你说的爱我，还在回味你给过的温柔！"
//        label.sizeToFit()
        
        label.textColor = UIColor.lightGrayColor()
        label.numberOfLines = 0
      return label
    }()
    
    var placeHolder : String? {
        didSet {
        
            placeHolderLabel.text = placeHolder
            
        }
    }
    
    
    override var font : UIFont? {
    
        didSet {
            if font == nil{
            
                return
                
            }
            
            placeHolderLabel.font = font
            
        }
        
    }
    
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setUpui()
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textchange", name: UITextViewTextDidChangeNotification, object: nil)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpui() {
    
        addSubview(placeHolderLabel)
        
        placeHolderLabel.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: placeHolderLabel, attribute: .Width, relatedBy: .Equal, toItem: self, attribute: .Width, multiplier: 1, constant: -10))
        
    }
    override func layoutSubviews() {
        super.layoutSubviews()
        placeHolderLabel.frame.origin.x = 5
        placeHolderLabel.frame.origin.y = 8
    }
    
    @objc private func textchange() {
    
        placeHolderLabel.hidden = hasText()
        
    }
    deinit {
    
        NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
}
